package com.ebanma.cloud.user.web;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ebanma.cloud.constant.RedisConstant;
import com.ebanma.cloud.dto.AjaxResult;
import com.ebanma.cloud.dto.UserDTO;
import com.ebanma.cloud.user.dao.po.User;
import com.ebanma.cloud.user.service.IUserService;
import com.ebanma.cloud.utils.RedisUtil;
import com.ebanma.cloud.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wangxin
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private IUserService iUserService;

    @GetMapping("/test")
    AjaxResult test() {
        System.out.println("222");
        QueryWrapper<User> qw = new QueryWrapper<>();
        return AjaxResult.success(200, "2", null);
    }

    @RequestMapping("/add")
    public AjaxResult addUser(@RequestBody UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        User me = UserHolder.getUser();
        user.setCreateUser(me.getId());
        iUserService.save(user);
        return AjaxResult.success(200, "新增成功 ", user);
    }

    /**
     * 校验验证码
     * 注册用户，用于校验前端传来的验证码,生成唯一账号
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/regist")
    AjaxResult regist(@RequestBody UserDTO userDTO) {

        return iUserService.regist(userDTO);
    }

    /**
     * 根据用户的主键id进行数据更新
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/update")
    AjaxResult updateUser(@RequestBody UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        iUserService.updateById(user);
        return AjaxResult.success(200, "修改成功", user);
    }

    @PostMapping("/loginByPhone")
    AjaxResult loginByPhone(@RequestBody UserDTO userDTO) {

        return iUserService.loginByPhone(userDTO);
    }

    /**
     * 获取验证码
     *
     * @return
     */
    @GetMapping("/code")
    public AjaxResult code(@RequestParam String phone) {
        //不允许60秒内连续发送两次请求

        // 自定义纯数字的验证码（随机4位数字，可重复）
        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 6);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        lineCaptcha.setGenerator(randomGenerator);
        // 重新生成code
        lineCaptcha.createCode();
        String code = lineCaptcha.getCode();
        String codeKey = RedisConstant.CAPTCHA_CODE + phone;
        //验证码保存到redis
        redisUtil.setEx(codeKey, code, 60L, TimeUnit.SECONDS);
        //三方平台返回验证码

        return AjaxResult.success(200, "获取验证码成功", null);
    }

}
